package com.sfcms.consumer.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
import java.util.Base64;

@Configuration
public class RestConfig {

    @Value("${provider.user}")
    private String name;

    @Value("${provider.password}")
    private String password;

    @Bean
    RestTemplate restTemplate()
    {
        return new RestTemplate();
    }

    @Bean   // 要进行一个Http头信息配置
    HttpHeaders getHeaders()
    {
        HttpHeaders headers=new HttpHeaders(); // 定义一个HTTP的头信息
        //String auth="root:root"; //认证的原始信息
        String auth=name+":"+password;
        byte[] encodedAuth = Base64.getEncoder()
                .encode(auth.getBytes(Charset.forName("US-ASCII"))); // 进行一个加密的处理
        String authHeader = "Basic " + new String(encodedAuth);
        headers.set("Authorization", authHeader);
        return headers;
    }
}
